

import 'package:easy_localization/easy_localization.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'package:get/get.dart';
import 'package:smart_freezer/core/resources/app_config.dart';
import 'package:smart_freezer/core/resources/icon_names.dart';
import 'package:smart_freezer/core/resources/routers.dart';
import 'package:smart_freezer/core/utils/local_storage.dart';
import 'package:smart_freezer/core/utils/tr_extension.dart';

import '../../../generated/locale_keys.g.dart';

class SettingLanguage extends StatefulWidget {
  const SettingLanguage({Key? key}) : super(key: key);

  @override
  State<SettingLanguage> createState() => _SettingLanguageState();
}

class _SettingLanguageState extends State<SettingLanguage> {

  List _dataList = ["简体中文", "繁體中文", "English"];
  int _selectIndex = LocalStorage.getLanguageValue();

  var isDarkMode = LocalStorage.getThemeMode() == 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Colors.white,
        elevation: 0,
        leadingWidth: 50,
        leading: InkWell(
          onTap: () {
            Get.back();
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 9, 0, 9),
            child: Icon(Icons.arrow_back_ios_rounded),
          ),
        ),
        title: Text(LocaleKeys.set_up_setting_language.trc(), ),
        centerTitle: true,
      ),
      body: Container(
        width: MainCommon.screenWidth,
        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
        color: isDarkMode ? Colors.black : MainCommon.bgGreyColor,
        child: ListView.builder(
          itemCount: _dataList.length,
            itemBuilder: (context, index) {
              final title = _dataList[index];
              return InkWell(
                onTap: () {
                  setState(() {
                    _selectIndex = index + 1;
                  });

                  LocalStorage.setLanguage(_selectIndex);
                  // 设置本地语言
                  final locale = context.supportedLocales[index];
                  context.setLocale(locale);
                  Get.updateLocale(locale);
                  Get.offNamed(LCRouter.home);
                },
                child: Container(
                  color: LocalStorage.getThemeMode() == 1 ? MainCommon.darkBlueMaterialColor : Colors.white,  // Color(0xFFF1F3F8),
                  padding: EdgeInsets.symmetric(horizontal: 15),
                  height: 50,
                  child: Column(
                    children: [
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(title, style: TextStyle(fontWeight: FontWeight.bold),),
                            Image.asset(
                                _selectIndex - 1 == index ? IconNames.setting_selected: IconNames.setting_unselected,
                              width: 25,
                            ),
                          ],
                        ),
                      ),
                      Divider(
                        height: 1,
                        thickness: 0,
                        indent: 5,
                        endIndent: 5,
                        // color: Color(0xFFD9D9D9),
                      ),

                    ],
                  ),
                ),
              );
            }
        ),
      ),
    );
  }
}
